package wnhz.com.business.freeshop.activity.fristfree;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseFragment;
import wnhz.com.business.groupshop.fragment.SearchAllFragment;
import wnhz.com.business.groupshop.fragment.SearchStoreFragment;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.view.TranslucentActionBar;
import wnhz.com.business.view.TranslucentActionBarW;

/**
 * 热搜——搜索界面
 */
public class SearchActivity extends BaseActivity implements ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBarW actionbar;
    @BindView(R.id.sliding_tabs)
    TabLayout slidingTabs;
    public String tabTitles[] = new String[]{"全部", "店铺"};
    @BindView(R.id.framelayout)
    FrameLayout framelayout;

    private BaseFragment baseFragment;
    private SearchAllFragment searchAllFragment;
    private SearchStoreFragment searchStoreFragment;
    private int type = 0;

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, SearchActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_f);
        ButterKnife.bind(this);
        actionbar.setData("", 0, null, 0, "取消", 2, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //改变状态栏颜色
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        initTabLayout();
        searchAllFragment = new SearchAllFragment();
        searchStoreFragment = new SearchStoreFragment();
        if (savedInstanceState != null) {
            baseFragment = (BaseFragment) getSupportFragmentManager().getFragment(savedInstanceState, baseFragment.getClass().getSimpleName());
        } else {
            switch (type) {
                case 0:
                    baseFragment = searchAllFragment.newInstance();
                    break;
                case 1:
                    baseFragment = searchStoreFragment.newInstance();
                    break;
            }
        }
        if (!baseFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.framelayout, baseFragment, baseFragment.getClass().getSimpleName()).show(baseFragment).commit();
        }


    }


    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {
        finish();

    }

    @Override
    public void onEditTextClick() {
        Log.e("test0913", "****  ");
    }


    /**
     * 加载数据
     */
    private void initTabLayout() {
        for (int i = 0; i < tabTitles.length; i++) {
            slidingTabs.addTab(slidingTabs.newTab().setText(tabTitles[i]));
        }


        slidingTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                initView();
                switch (tab.getPosition()) {
                    //全部
                    case 0:
                        turnToFragment(baseFragment.getClass(), SearchAllFragment.class, null);
                        break;
                    //店铺
                    case 1:
                        turnToFragment(baseFragment.getClass(), SearchStoreFragment.class, null);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void turnToFragment(Class<? extends BaseFragment> fromFragmentClass, Class<? extends BaseFragment> toFragmentClass, Bundle args) {

        FragmentManager fm = getSupportFragmentManager();
        //被切换的Fragment标签
        String fromTag = fromFragmentClass.getSimpleName();
        //切换到的Fragment标签
        String toTag = toFragmentClass.getSimpleName();
        //查找切换的Fragment
        Fragment fromFragment = fm.findFragmentByTag(fromTag);
        Fragment toFragment = fm.findFragmentByTag(toTag);
        //如果要切换到的Fragment不存在，则创建
        if (toFragment == null) {
            try {
                toFragment = toFragmentClass.newInstance();
                toFragment.setArguments(args);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        //如果有参数传递，
        if (args != null && !args.isEmpty()) {
            toFragment.getArguments().putAll(args);
        }
        //Fragment事务
        FragmentTransaction ft = fm.beginTransaction();
        //设置Fragment切换效果
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                android.R.anim.fade_in, android.R.anim.fade_out);
        /**
         * 如果要切换到的Fragment没有被Fragment事务添加，则隐藏被切换的Fragment，添加要切换的Fragment
         * 否则，则隐藏被切换的Fragment，显示要切换的Fragment
         */
        if (!toFragment.isAdded()) {
            ft.hide(fromFragment).add(R.id.framelayout, toFragment, toTag);
        } else {
            ft.hide(fromFragment).show(toFragment);
        }
        baseFragment = (BaseFragment) toFragment;
        //添加到返回堆栈
        //        ft.addToBackStack(tag);
        //不保留状态提交事务
        ft.commitAllowingStateLoss();
    }

}
